﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
using System.Security.Principal;
using ControlsLib.Filters;

namespace ControlsLib
{
    public partial class ReportManager2<T> : Form, IForm where T : class, new()
    {
        public event EventHandler<CustomEventArgs<T>> ButtonClicked;
        public event EventHandler<CustomEventArgs<T>> CustomButtonClicked;

        private ButtonsBar<T> m_buttonsBar = null;
        private bool m_useFilter = false;
        private bool m_enableSecurityOnData = false;
        private ReportParameter m_currentUserParameter = null;
        private List<ReportParameter> m_parameters = null;
        private FilterContainerBase2<T> m_filter = null;
        private bool m_useProgressIndicator = false;

        public List<ReportParameter> ReportParameters
        {
            get { return m_parameters; }
            set { m_parameters = value; }
        }

        public bool EnableSecurityOnData
        {
            get { return m_enableSecurityOnData; }
            set { m_enableSecurityOnData = value; }
        }

        public bool UseProgressIndicator
        {
            get { return m_useProgressIndicator; }
            set { m_useProgressIndicator = value; }
        }

        protected internal ReportViewer ReportViewer { get { return this.reportViewer1; } }
        
        protected internal FilterContainerBase2<T> Filter
        {
            get { return m_filter; }
            set 
            { 
                m_filter = value;

                if (m_useFilter && (m_filter != null))
                {
                    this.m_buttonsBar.GetFilterControl().TheContainer = m_filter.PopupContainerControl;
                    m_filter.ControlManager = this.m_buttonsBar.GetFilterControl();

                    this.m_buttonsBar.ShowFilterButton = true;
                }
            }
        }

        public bool UseFilter
        {
            get { return m_useFilter; }
            set
            {
                m_useFilter = value;

                if (this.m_buttonsBar != null)
                  this.m_buttonsBar.ShowFilterButton = value;
            }
        }

        public ReportManager2()
        {
            InitializeComponent();
        }

        public virtual void InitializeForm()
        {
            if (!this.DesignMode)
            {
                m_parameters = new List<ReportParameter>();

                m_buttonsBar = new ButtonsBar<T>();
                m_buttonsBar.Dock = DockStyle.Bottom;
                this.Controls.Add(m_buttonsBar);

                this.m_buttonsBar.ShowOnlyCustomButton = true;
                this.m_buttonsBar.InitializeButtonBar();

                m_buttonsBar.ButtonClicked += new EventHandler<CustomEventArgs<T>>(m_buttonsBar_ButtonClicked);
                m_buttonsBar.CustomButtonClicked += new EventHandler<CustomEventArgs<T>>(m_buttonsBar_CustomButtonClicked);
            }
        }

        void m_buttonsBar_CustomButtonClicked(object sender, CustomEventArgs<T> e)
        {
            if (CustomButtonClicked != null)
            {
                CustomButtonClicked(sender, e);
            }
        }

        void m_buttonsBar_ButtonClicked(object sender, CustomEventArgs<T> e)
        {
            switch (e.Action)
            {
                case Actions.Confirm:
                    this.Close();
                    break;
                case Actions.Cancel:
                    this.Close();
                    break;
                default:
                    break;
            }

            if (ButtonClicked != null)
            {
                ButtonClicked(sender, e);
            }
        }

        private void ReportManager2_Load(object sender, EventArgs e)
        {
            //this.reportViewer1.RefreshReport();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (m_useProgressIndicator)
            {
                //this.ReportViewer.Visible = false;
                this.ShowProgressIndicator(true);
                this.StartProgressIndicator();
            }
        }

        //protected override void OnShown(EventArgs e)
        //{
        //    base.OnShown(e);

        //    if (m_useProgressIndicator)
        //    {
        //        //this.ReportViewer.Visible = false;
        //        this.ShowProgressIndicator(true);
        //        this.StartProgressIndicator();
        //    }
        //}

        protected void SetUpReport(object datasource, string reportName)
        {
            if (datasource != null)
            {
                if (m_useProgressIndicator)
                {
                    //this.ReportViewer.Visible = true;
                    this.ShowProgressIndicator(false);
                    this.StopProgressIndicator();
                }

                //Microsoft.Reporting.WinForms.ReportViewer reportViewer = this.reportViewer1;
                //reportViewer1.ZoomPercent = 130;
                //reportViewer1.ZoomMode = ZoomMode.Percent;

                reportViewer1.LocalReport.DataSources.Clear();

                // Set Processing Mode
                reportViewer1.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local;

                // Set RDL file
                reportViewer1.LocalReport.ReportPath = string.Format(@"{0}\Reports\{1}.rdlc", Application.StartupPath, reportName);

                //if (m_enableSecurityOnData)
                //{
                //    IPrincipal m_currentUser = System.Threading.Thread.CurrentPrincipal;

                //    m_parameters.Add(new ReportParameter("IsAdminRole", (m_currentUser.IsInRole("Amministrazione") ? "true" : "false")));
                    
                //    //List<ReportParameter> parameters = new List<ReportParameter>();
                //    //parameters.Add(new ReportParameter("IsAdminRole", (m_currentUser.IsInRole("Amministrazione") ? "true" : "false")));
                //    //this.reportViewer1.LocalReport.SetParameters(parameters);
                //}

                if (m_enableSecurityOnData)
                {
                    IPrincipal m_currentUser = System.Threading.Thread.CurrentPrincipal;

                    m_currentUserParameter = new ReportParameter("IsAdminRole", (m_currentUser.IsInRole("Amministrazione") ? "true" : "false"));
                }

                if (m_parameters != null)
                {
                    if (m_currentUserParameter != null)
                    {
                        m_parameters.Add(m_currentUserParameter);
                    }
                }
                else
                {
                    if (m_currentUserParameter != null)
                    {
                        if(m_parameters == null) m_parameters = new List<ReportParameter>();
                        m_parameters.Add(m_currentUserParameter);
                    }
                }

                if (m_parameters != null)
                {
                    this.reportViewer1.LocalReport.SetParameters(m_parameters);
                }

                // Supply data corresponding to each report data source.
                reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("ReportData", datasource));

                // Process and render the report
                reportViewer1.RefreshReport();
            }
            else
            {
                this.reportViewer1.LocalReport.DataSources.Clear();

                MessageBox.Show("Nessun dato trovato!");

                if (m_useProgressIndicator)
                {
                    //this.ReportViewer.Visible = true;
                    this.ShowProgressIndicator(false);
                    this.StopProgressIndicator();
                }
            }
        }

        protected void SetUpReport(object datasource, string reportName, List<ReportParameter> parameters)
        {
            m_parameters = parameters;

            SetUpReport(datasource, reportName);
        }

        public void ResetParameters()
        {
            if (m_parameters != null)
                m_parameters.Clear();

            this.reportViewer1.Reset();
        }

        public void AddParameter(string name, string value)
        {
            if (m_parameters == null)
            {
                m_parameters = new List<ReportParameter>();
            }

            ReportParameter p = new ReportParameter(name, value);
            if (!m_parameters.Contains(p))
                m_parameters.Add(p);
        }

        public void AddParameter(string name, string[] values)
        {
            if (m_parameters == null)
            {
                m_parameters = new List<ReportParameter>();
            }

            ReportParameter p = new ReportParameter(name, values);
            if (!m_parameters.Contains(p))
                m_parameters.Add(p);
        }


        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);

            this.progressIndicator1.Left = (this.ClientSize.Width - this.progressIndicator1.Width) / 2;
            this.progressIndicator1.Top = (this.ClientSize.Height - this.progressIndicator1.Height) / 2;
        }

        public void AddCustomButton(string name, string text)
        {
            this.m_buttonsBar.AddCustomButton(name, text);
        }

        #region Progress Indicator Management...
        public void ShowProgressIndicator(bool show)
        {
            this.progressIndicator1.Visible = show;
        }

        public void StartProgressIndicator()
        {
            this.progressIndicator1.Start();
        }

        public void StopProgressIndicator()
        {
            this.progressIndicator1.Stop();
        }
        #endregion
    }
}
